import { ref } from 'vue'
import { defineStore } from 'pinia'
import { loginAPI, loginTouristAPI } from '@/api/user.js'

export const useUserStore = defineStore(
  'user',
  () => {
    const user = ref({})

    // 用户登录
    const login = async (params) => {
      const result = await loginAPI(params)
      user.value = result
    }
    // 修改用户信息
    const updateUser = (params) => {
      Object.assign(user.value, params)
    }
    // 游客登录
    const loginTourist = async () => {
      const result = await loginTouristAPI()
      user.value = result
    }

    // 退出登录
    const logout = () => {
      user.value = {}
    }

    return { user, login, loginTourist, logout, updateUser }
  },
  {
    persist: true
  }
)
